home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / modes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-06  |  4.9 KB  |  202 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. #include "cf.defs.h"
  28. #include "cf.extern.h"
  29.  
  30.  
  31. /***************************************************************/
  32. /* Modestring toolkit                                          */
  33. /***************************************************************/
  34.  
  35.   enum modestate
  36.      {
  37.      who,
  38.      which
  39.      };
  40.  
  41. ParseModeString(modestring,plusmask,minusmask)
  42.  
  43. char *modestring;
  44. mode_t *plusmask, *minusmask;
  45.  
  46. { char modebuffer[maxvarsize], *sp; 
  47.  int affected = 0, value = 0, gotaction;
  48.   char action = '=';
  49.   enum modestate state = who;
  50.  
  51. if (modestring == NULL)
  52.    {
  53.    return;
  54.    }
  55.  
  56. Debug1("ParseModeString(%s)\n",modestring);
  57.  
  58. gotaction = false;
  59. *plusmask = *minusmask = 0;
  60.  
  61. for (sp = modestring; true ; *sp++)
  62.    {
  63.    switch (*sp)
  64.       {
  65.       case 'a': CheckModeState(who,state,*sp);
  66.                 affected |= 07777;
  67.                 break;
  68.  
  69.       case 'u': CheckModeState(who,state,*sp);
  70.                 affected |= 04700;
  71.                 break;
  72.  
  73.       case 'g': CheckModeState(who,state,*sp);
  74.                 affected |= 02070;
  75.                 break;
  76.  
  77.       case 'o': CheckModeState(who,state,*sp);
  78.                 affected |= 00007;
  79.                 break;
  80.  
  81.       case '+':
  82.       case '-':
  83.       case '=': if (gotaction)
  84.                    {
  85.                    yyerror("Too many +-= in mode string");
  86.                    }
  87.                 action = *sp;
  88.                 state = which;
  89.                 gotaction = true;
  90.                 break;
  91.  
  92.       case 'r': CheckModeState(which,state,*sp);
  93.                 value |= 0444 & affected;
  94.                 break;
  95.  
  96.       case 'w': CheckModeState(which,state,*sp);
  97.                 value |= 0222 & affected;
  98.                 break;
  99.  
  100.       case 'x': CheckModeState(which,state,*sp);
  101.                 value |= 0111 & affected;
  102.                 break;
  103.  
  104.       case 's': CheckModeState(which,state,*sp);
  105.                 value |= 06000 & affected;
  106.                 break;
  107.  
  108.       case 't': CheckModeState(which,state,*sp);
  109.                 value |= 01000;
  110.                 break;
  111.  
  112.       case '0':
  113.       case '1':
  114.       case '2':
  115.       case '3':
  116.       case '4':
  117.       case '5':
  118.       case '6':
  119.       case '7': state = which;
  120.                 sscanf(sp,"%o",&value);
  121.                 while (isdigit(*sp) && (*sp != '\0'))
  122.            {
  123.                    sp++;
  124.                    }
  125.                 sp--;
  126.                 break;
  127.  
  128.       case ',':
  129.                 SetMask(action,value,plusmask,minusmask);
  130.                 action = '=';
  131.                 affected = 0;
  132.                 value = 0;
  133.                 gotaction = false;
  134.                 state = who;
  135.                 break;
  136.  
  137.       case '\0':
  138.                 if (state == who || value == 0)
  139.                    {
  140.                    Warning("mode string is incomplete");
  141.                    }
  142.  
  143.                 SetMask(action,value,plusmask,minusmask);
  144.                 Debug1("[PLUS=%o][MINUS=%o]\n",*plusmask,*minusmask);
  145.                 return;
  146.  
  147.       default:  yyerror ("Invalid mode string");
  148.                 break;
  149.       }
  150.    }
  151. }
  152.  
  153. /*********************************************************/
  154.  
  155. CheckModeState(stateA,stateB,ch)
  156.  
  157. enum modestate stateA;
  158. int stateB;
  159. char ch;
  160.  
  161. {
  162. if ((int)stateA != stateB)
  163.    {
  164.    sprintf(VBUFF,"Mode string constant (%c) used out of context",ch);
  165.    yyerror(VBUFF);
  166.    }
  167. return;
  168. }
  169.  
  170. /*********************************************************/
  171.  
  172. SetMask(action,value,p,m)
  173.  
  174. char action;
  175. int value;
  176. mode_t *p,*m;
  177.  
  178. {
  179. Debug1("SetMask(%c%o)\n",action,value);
  180.  
  181. switch(action)
  182.    {
  183.    case '+':
  184.              *p |= value;
  185.              *m |= 0;
  186.              return;
  187.    case '-':
  188.              *p |= 0;
  189.              *m |= value;
  190.              return;
  191.    case '=':
  192.              *p |= value;
  193.              *m |= (~value) & 07777;
  194.              return;
  195.    default:
  196.              sprintf(VBUFF,"Mode directive %c is unknown",action);
  197.              yyerror(VBUFF);
  198.              return;
  199.    }
  200. }
  201.  
  202.